package objectcreate;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;

public class Objectcreate {

    public static void main(String[] args) throws FileNotFoundException {
        File file = new File("GameList.txt");
        int inputNumeric = 1;
        char inputChar = 'j';
        ArrayList<Videogame> games = new ArrayList();
        Scanner scanfile = new Scanner(file);
        Scanner keyboard = new Scanner(System.in);
        String input;
        while (true) {
            System.out.println("\nWelcome to The Database");
            System.out.println("To see what games are in the data base, input \"Games\"");
            System.out.println("To input a new game, input \"New\"");
            System.out.println("To leave The Database, input \"End Program\".... but why would you want to leave?");
            System.out.println("To export The Database's game list, input \"export\"");
            System.out.println("To import a game list from \"GameList.txt\", input \"import\"");
            input = keyboard.nextLine();
            if ("Games".equalsIgnoreCase(input)) {
                for (int i = 0; i < games.size(); i++) {
                    System.out.println("\n" + games.get(i).printgame());
                }
            } else if ("new".equalsIgnoreCase(input)) {
                System.out.println("\nWelcome to The \"New Game\" section of The Database. \nPlease input the genre of game");
                input = keyboard.nextLine();



                if ("RPG".equalsIgnoreCase(input)) {
                    RPG g = new RPG();
                    System.out.println("NEW RPG SPACE CREATED. Please input name of game");
                    input = keyboard.nextLine();
                    g.name = input;
                    System.out.println("NAME INPUT ACCEPTED. Please input rating of game");
                    inputChar = keyboard.nextLine().charAt(0);
                    g.rating = inputChar;
                    System.out.println("RATING ACCEPTED. Please input maximum level");
                    inputNumeric = keyboard.nextInt();
                    g.maxLevel = inputNumeric;
                    System.out.println("MAXIMUM LEVEL RECORDED. Please input exp formula");
                    input = keyboard.nextLine();
                    g.expPerLevel = input;
                    input = keyboard.nextLine();
                    System.out.println("Thank you for recording this game.");
                    games.add(g);
                } else if ("platformer".equalsIgnoreCase(input) || "Platform Game".equalsIgnoreCase(input)) {
                    Platformer l = new Platformer();
                    System.out.println("NEW PLATFORMER SPACE CREATED. Please input name of game");
                    input = keyboard.nextLine();
                    l.name = input;
                    System.out.println("NAME INPUT ACCEPTED. Please input rating of game");
                    inputChar = keyboard.nextLine().charAt(0);
                    l.rating = inputChar;
                    System.out.println("RATING ACCEPTED. Please input starting lives");
                    inputNumeric = keyboard.nextInt();
                    l.lives = inputNumeric;
                    System.out.println("Thank you for recording this game");
                    games.add(l);
                } else if ("arcade".equalsIgnoreCase(input) || "Arcade Game".equalsIgnoreCase(input)) {
                    Arcade d = new Arcade();
                    System.out.println("NEW ARCADE GAME SPACE CREATED. Please input name of game");
                    input = keyboard.nextLine();
                    d.name = input;
                    System.out.println("NAME INPUT ACCEPTED. Please input rating of game");
                    inputChar = keyboard.nextLine().charAt(0);
                    d.rating = inputChar;
                    System.out.println("RATING ACCEPTED. Please input maximum score");
                    inputNumeric = keyboard.nextInt();
                    d.maxScore = inputNumeric;
                    games.add(d);
                } else if ("fps".equalsIgnoreCase(input) || "RTS".equalsIgnoreCase(input)) {
                    System.out.println("I appologize, but I do not currently have that genre availible.");
                } else {
                    System.err.println("ERROR=NOT ACTUAL GENRE: " + input);


                }

            } else if ("end".equalsIgnoreCase(input) || "end program".equalsIgnoreCase(input)) {

                System.exit(0);
            } else if ("export".equalsIgnoreCase(input)) {


                PrintWriter pw = new PrintWriter(file);
                for (int i = 0; i < games.size(); i++) {
                    pw.println(games.get(i).export());


                }
                pw.close();
                System.out.println("\nDone. File name is: GameList.txt");
            } else if ("import".equalsIgnoreCase(input)) {
                while (scanfile.hasNextLine()) {
                    String[] imp;
                    String splitter = ",";
                    String t;
                    t = scanfile.nextLine();
                    imp = t.split(splitter);
                    if (imp[0].equalsIgnoreCase("rpg")) {
                        RPG rpg = new RPG();
                        rpg.name = imp[1];
                        rpg.rating = imp[2].charAt(0);
                        rpg.maxLevel = Integer.parseInt(imp[3]);
                        rpg.expPerLevel = imp[4];
                        games.add(rpg);
                    } else if (imp[0].equalsIgnoreCase("Arcade")) {
                        Arcade ar = new Arcade();
                        ar.name = imp[1];
                        ar.rating = imp[2].charAt(0);
                        ar.maxScore = Integer.parseInt(imp[3]);
                        games.add(ar);
                    } else if (imp[0].equalsIgnoreCase("platformer")) {
                        Platformer pl = new Platformer();
                        pl.name = imp[1];
                        pl.rating = imp[2].charAt(0);
                        pl.lives = Integer.parseInt(imp[3]);
                        games.add(pl);
                    }
                } 
            }
            
            else {
                System.out.println("ERROR=Unknown Command. I don't understand the input");
            }
        }
    }
}
